package wechat.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wechat.dao.JSTicketDao;
import wechat.entity.AccessTokenEntity;
import wechat.entity.JSTicketEntity;
import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.AccessTokenService;
import wechat.service.JSTicketService;
import wechat.service.base.BaseRequest;
import wechat.util.date.DateUtil;

@Service
public class IJSTicketService extends BaseRequest implements JSTicketService {

	private Logger log = LoggerFactory.getLogger(IJSTicketService.class);

	@Autowired
	private AccessTokenService accessTokenService;
	
	@Autowired
	private JSTicketDao jsTicketDao;

	public JSTicketEntity getJSTicket(String appid) throws Exception {
		log.info("IJSTicketService--getJSTicket appid=" + appid);

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		JSTicketEntity newJSTicket = jsTicket(instanceResource);
		
		return newJSTicket;

	}
	
	public JSTicketEntity getJSTicket(InstanceResource instanceResource) throws Exception {
		JSTicketEntity newJSTicket = jsTicket(instanceResource);
		
		return newJSTicket;
	}

	
	public void resetJSTicket(String appid) throws Exception {
		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		
		long currentTime = DateUtil.getCurrentTime();
		JSTicketEntity newJSTicket = new JSTicketEntity();
		
		AccessTokenEntity accessTokenEntity = accessTokenService.getAccessToken(instanceResource);
		String jSTicket = super.fetchJSTicket(accessTokenEntity.getAccessToken());
		
		newJSTicket.setjSTicket(jSTicket);
		newJSTicket.setModifyTime(currentTime);

		jsTicketDao.updateJSTicket(newJSTicket);
		
	}

	private JSTicketEntity jsTicket(InstanceResource instanceResource) throws Exception {
//		String appid = instanceResource.getAppid();
		
		long currentTime = DateUtil.getCurrentTime();
		
		JSTicketEntity newJSTicket = new JSTicketEntity();
		String jSTicket = null;

		JSTicketEntity jsTicketEntity = jsTicketDao.getJSTicket();
		if (jsTicketEntity == null) {
			AccessTokenEntity accessTokenEntity = accessTokenService.getAccessToken(instanceResource);
			jSTicket = super.fetchJSTicket(accessTokenEntity.getAccessToken());
			
			newJSTicket.setjSTicket(jSTicket);
			newJSTicket.setCreateTime(currentTime);
			newJSTicket.setModifyTime(currentTime);
			jsTicketDao.addJSTicket(newJSTicket);

			log.info("IJSTicketService--jsTicket Get jSTicket jSTicket=" + jSTicket);
		}
		else {
			long modifyTime = jsTicketEntity.getModifyTime();
			boolean timeOut = (currentTime - modifyTime) > super.Deadline * 1000;
			
			newJSTicket = jsTicketEntity;

			if (timeOut) {
				AccessTokenEntity accessTokenEntity = accessTokenService.getAccessToken(instanceResource);
				jSTicket = super.fetchJSTicket(accessTokenEntity.getAccessToken());
				
				newJSTicket.setjSTicket(jSTicket);
				newJSTicket.setModifyTime(currentTime);

				jsTicketDao.updateJSTicket(newJSTicket);

				log.info("IJSTicketService--jsTicket Get jSTicket jSTicket=" + jSTicket);
			}
		}
		
		log.info("IJSTicketService--jsTicket Get jSTicket  jSTicket=" + newJSTicket);
		return newJSTicket;
	}



}
